package cn.edu.scau.cmi.wujiaming.experiment5.consistent;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.TreeMap;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class ConsistentComponentController implements Initializable {
	/**
	 * 关联的内容 名字对应的对象
	 */
	Map<String, ConsistentComponent> ccs = new HashMap<>();
	/**
	 * 用于展示"现有的目录结构"
	 */
	Map<String, List<ConsistentComponent>> display = new TreeMap<>();
	/**
	 * 用于查看有几个关联分支
	 */
	int mapIndex;

	@FXML
	private TextField tf01;

	@FXML
	private TextField tf02;

	@FXML
	private TextField tf03;

	@FXML
	private ComboBox<String> cb;

	@FXML
	private TextArea ta;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {

		ObservableList<String> items = FXCollections.observableArrayList("添加", "删除", "杀毒");
		cb.setItems(items);

	}

	private boolean isCanCreated(String objectName) {
		tf01.clear();
		tf01.setPromptText("输入名字");
		if (objectName.equals("") || objectName.equals("输入名字")) {
			ta.setText("创建失败！！！");
			return false;
		} else {
			if (ccs.containsKey(objectName)) {
				ta.setText("已经存在该对象！！！");
				return false;
			} else {
				return true;
			}
		}
	}

	public void OnButtonDisplay01(ActionEvent event) {
		String objectName = tf01.getText();
		if (isCanCreated(objectName)) {
			ccs.put(objectName, new ConsistentLeaf(objectName));
			OnButtonDisplay04(event);
		}
	}

	public void OnButtonDisplay02(ActionEvent event) {
		String objectName = tf01.getText();
		if (isCanCreated(objectName)) {
			ccs.put(objectName, new ConsistentComposite(objectName));
			OnButtonDisplay04(event);
		}
	}

	public void OnButtonDisplay03(ActionEvent event) {
		String objectName01 = tf02.getText(); // 对这个对象进行操作
		String objectName02 = tf03.getText(); // 这个对象被添加、删除
		if (cb.getValue() != null) {
			switch (cb.getValue()) {
			case "添加":
				if (ccs.containsKey(objectName01) && ccs.containsKey(objectName02)) {
					ta.setText(ccs.get(objectName01).add(ccs.get(objectName02)));
				} else {
					ta.setText("添加失败！！！");
				}
				break;
			case "删除":
				if (ccs.containsKey(objectName01) && ccs.containsKey(objectName02)) {
					ta.setText(ccs.get(objectName01).remove(ccs.get(objectName02)));
				} else {
					ta.setText("删除失败！！！");
				}
				break;
			case "杀毒":
				if (ccs.containsKey(objectName01)) {
					ta.setText(ccs.get(objectName01).doSomething(false, ""));
				} else {
					ta.setText("对" + objectName01 + "进行杀毒失败！！！");
				}
				break;
			default:
			}
		}
	}

	public void OnButtonDisplay04(ActionEvent event) {
		display.clear();
		display.put("unrelated", new ArrayList<>());
		for (ConsistentComponent temp : ccs.values()) {
			// 如果没有被关联，既没有父节点
			if (temp.getFather() == null) {
				display.get("unrelated").add(temp);
			} else {
				if (temp.getFather().getFather() != null) {
					if (temp.getFather().getName().equals(temp.getFather().getFather().getName())) {
						temp.setFather(temp.getFather().getFather());
					}
				}
				// 如果没有生成父节点的List
				if (!display.containsKey(temp.getFather().getName())) {
					display.put(temp.getFather().getName(), new ArrayList<>());
				}
				display.get(temp.getFather().getName()).add(temp);
			}
		}
		StringBuilder content = new StringBuilder();
		String tab = "";
		for (String temp : display.keySet()) {
			Iterator<ConsistentComponent> iterator = display.get(temp).iterator();
			if (temp.equals("unrelated")) {
				content.append("unrelated object:\n");
				if (!iterator.hasNext())
					content.append("none\n");
				else
					while (iterator.hasNext()) {
						content.append(iterator.next().getName() + "\n");
					}
			} else {
				content.append("\n");
				while (iterator.hasNext()) {
					ConsistentComponent t = iterator.next();
					if (t.getName().equals(temp)) {
						content.append(t.doSomething(true, tab));
					}
				}
				content.append("\n");
			}
		}
		ta.setText(content.toString());
	}
}